package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Asignaturaxcompetencia;
import com.lostreseditores.sabersai.model.dto.AsignaturaxcompetenciaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class AsignaturaxcompetenciaView {
    private InputText txtAsigidn_Asignatura;
    private InputText txtComciadn_Competencia;
    private InputText txtAsigxcomciaidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<AsignaturaxcompetenciaDTO> data;
    private AsignaturaxcompetenciaDTO selectedAsignaturaxcompetencia;

    public AsignaturaxcompetenciaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            AsignaturaxcompetenciaDTO asignaturaxcompetenciaDTO = (AsignaturaxcompetenciaDTO) e.getObject();

            if (txtAsigidn_Asignatura == null) {
                txtAsigidn_Asignatura = new InputText();
            }

            txtAsigidn_Asignatura.setValue(asignaturaxcompetenciaDTO.getAsigidn_Asignatura());

            if (txtComciadn_Competencia == null) {
                txtComciadn_Competencia = new InputText();
            }

            txtComciadn_Competencia.setValue(asignaturaxcompetenciaDTO.getComciadn_Competencia());

            if (txtAsigxcomciaidn == null) {
                txtAsigxcomciaidn = new InputText();
            }

            txtAsigxcomciaidn.setValue(asignaturaxcompetenciaDTO.getAsigxcomciaidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtAsigidn_Asignatura != null) {
            txtAsigidn_Asignatura.setValue(null);
            txtAsigidn_Asignatura.setDisabled(true);
        }

        if (txtComciadn_Competencia != null) {
            txtComciadn_Competencia.setValue(null);
            txtComciadn_Competencia.setDisabled(true);
        }

        if (txtAsigxcomciaidn != null) {
            txtAsigxcomciaidn.setValue(null);
            txtAsigxcomciaidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Asignaturaxcompetencia entity = null;

        try {
            Integer asigxcomciaidn = new Integer(txtAsigxcomciaidn.getValue()
                                                                  .toString());
            entity = BusinessDelegatorView.getAsignaturaxcompetencia(asigxcomciaidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtAsigidn_Asignatura.setDisabled(false);
            txtComciadn_Competencia.setDisabled(false);
            txtAsigxcomciaidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtAsigidn_Asignatura.setValue(entity.getAsignatura().getAsigidn());
            txtAsigidn_Asignatura.setDisabled(false);
            txtComciadn_Competencia.setValue(entity.getCompetencia()
                                                   .getComciadn());
            txtComciadn_Competencia.setDisabled(false);
            txtAsigxcomciaidn.setValue(entity.getAsigxcomciaidn());
            txtAsigxcomciaidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveAsignaturaxcompetencia(FacesUtils.checkInteger(
                    txtAsigxcomciaidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtComciadn_Competencia));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteAsignaturaxcompetencia(FacesUtils.checkInteger(
                    txtAsigxcomciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateAsignaturaxcompetencia(FacesUtils.checkInteger(
                    txtAsigxcomciaidn),
                FacesUtils.checkInteger(txtAsigidn_Asignatura),
                FacesUtils.checkInteger(txtComciadn_Competencia));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtAsigxcomciaidn == null) {
                txtAsigxcomciaidn = new InputText();
            }

            txtAsigxcomciaidn.setValue(selectedAsignaturaxcompetencia.getAsigxcomciaidn());

            BusinessDelegatorView.deleteAsignaturaxcompetencia(FacesUtils.checkInteger(
                    txtAsigxcomciaidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedAsignaturaxcompetencia);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(Integer asigxcomciaidn,
        Integer asigidn_Asignatura, Integer comciadn_Competencia)
        throws Exception {
        try {
            BusinessDelegatorView.updateAsignaturaxcompetencia(asigxcomciaidn,
                asigidn_Asignatura, comciadn_Competencia);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("AsignaturaxcompetenciaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtAsigidn_Asignatura() {
        return txtAsigidn_Asignatura;
    }

    public void setTxtAsigidn_Asignatura(InputText txtAsigidn_Asignatura) {
        this.txtAsigidn_Asignatura = txtAsigidn_Asignatura;
    }

    public InputText getTxtComciadn_Competencia() {
        return txtComciadn_Competencia;
    }

    public void setTxtComciadn_Competencia(InputText txtComciadn_Competencia) {
        this.txtComciadn_Competencia = txtComciadn_Competencia;
    }

    public InputText getTxtAsigxcomciaidn() {
        return txtAsigxcomciaidn;
    }

    public void setTxtAsigxcomciaidn(InputText txtAsigxcomciaidn) {
        this.txtAsigxcomciaidn = txtAsigxcomciaidn;
    }

    public List<AsignaturaxcompetenciaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataAsignaturaxcompetencia();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(
        List<AsignaturaxcompetenciaDTO> asignaturaxcompetenciaDTO) {
        this.data = asignaturaxcompetenciaDTO;
    }

    public AsignaturaxcompetenciaDTO getSelectedAsignaturaxcompetencia() {
        return selectedAsignaturaxcompetencia;
    }

    public void setSelectedAsignaturaxcompetencia(
        AsignaturaxcompetenciaDTO asignaturaxcompetencia) {
        this.selectedAsignaturaxcompetencia = asignaturaxcompetencia;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
